// pages/operat/codeManager/codeApply/codeApply.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeApply: {
     AllotProductName: "",
      ProductId: "",
      // EnterpriseId: LApp.globalData.userInfo.EnterpriseId,
      //  UserId: LApp.globalData.userInfo.UserID,
     
    },
    codeTypes: [],
    areaIndex: 0,
     directSubLevelCo: [],
     productSelectItem:[]
 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.codeTypes = [
      { Code: 1, Name: '服务码', checked: false },
      { Code: 2, Name: '管理码', checked: false }
    ];
    this.getProductSelectItemList();
    this.getEnterpriseList();
    this.data.codeApply.ApplyUserName = LApp.globalData.userInfo.EnterpriseName;
    this.data.codeApply.EnterpriseId=LApp.globalData.userInfo.EnterpriseId;
    this.data.codeApply.UserId = LApp.globalData.userInfo.UserID;
    
        this.setData(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 保存按钮
   */
  onSaveClick: function (e) {
    LApp._post({
      url: "nbapi/codemanager/code/apply",
      data: this.data.codeApply,
      success: function (r) {
        wx.showModal({
          title: '提交成功',
          content: '请等待审核通过',
          showCancel: false,
          success: function () {
            //刷新父级列表
            wx.$$getParent().getUserCodeApplyList();
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    })
  },
  checkboxChange: function (e) {
    this.data.codeApply.CodeFunc = e.detail.value.join(",");
    for (var i = 0; i < this.data.codeTypes.length; i++) {
      if (this.data.codeApply.CodeFunc.indexOf(this.data.codeTypes[i].Code) != -1) {
        this.data.codeTypes[i].checked = true;
      } else { this.data.codeTypes[i].checked = false; }
    }
  },
  codeApplyModelBind: function (e) {
    wx.$$apply(e, this.data.codeApply);
  },
  getProductSelectItemList: function () {
    let _this = this;
  
    LApp._get({
      url: "nbapi/product/selectitem",
      data: { enterpriseId: LApp.globalData.userInfo.EnterpriseId },
      success: function (r) {
        _this.data.productSelectItem = r;
        // if (!_this.data.codeApply.AllotProductName) {
        //  _this.data.codeApply.AllotProductName = r[0].Name;
        //   _this.data.codeApply.ProductId = r[0].Code;
        // }
        _this.setData(_this.data);
      }
    })
  },
 
  productNameSelect: function (e) {
   if (this.data.productSelectItem.length>0){
    this.data.areaIndex = e.detail.value;
    this.data.codeApply.AllotProductName = this.data.productSelectItem[this.data.areaIndex].Name;
    this.data.codeApply.ProductId = this.data.productSelectItem[this.data.areaIndex].Code;
    this.setData(this.data);
   }
  }
,
 /**
    * 获取企业列表
    */
 
 getEnterpriseList: function () {
  let _this = this;

  LApp._get({
   url: "nbapi/codemanager/code/apply/topenterparise",
   data: { enterpriseId: LApp.globalData.userInfo.EnterpriseId },
   success: function (r) {
    _this.data.directSubLevelCo = r;
    if (!_this.data.codeApply.F_EnterpriseName) {
     _this.data.codeApply.F_EnterpriseName = r[0].Name;
     _this.data.codeApply.F_EnterpriseId = r[0].Code;
    }
    _this.setData(_this.data);
   
   },
     enterpriseSelect: function (e) {
      if (this.data.directSubLevelCo.length > 0) {
      this.data.areaIndex = e.detail.value;
      this.data.codeApply.F_EnterpriseName = this.data.directSubLevelCo[this.data.areaIndex].Name;
      this.data.codeApply.F_EnterpriseId = this.data.directSubLevelCo[this.data.areaIndex].Code;
      this.setData(this.data);
      }
     },

   
  })
 }
})